package cate.game.activity.optionalgift.po;

import cate.common.table.activity.optionalgift.row.OptionalGiftBaseRow;
import cate.common.table.activity.optionalgift.row.OptionalGiftPoolRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.optionalgift.OptionalGiftActivity;
import cate.game.res.MixRes;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

import java.beans.Transient;
import java.util.HashMap;
import java.util.Map;

public class OptionalGiftPo extends SubActivitySingle<OptionalGiftActivity> {
    @NoteField("配置id")
    public int id;
    @NoteField(value = "自选奖励",detail = "key为格子编号，value为奖励池配置id")
    public Map<Integer,Integer> rewards;
    @NoteField("已购买次数")
    public int times;

    public OptionalGiftPo() {
    }

    public OptionalGiftPo(int id, int times) {
        this.id = id;
        this.times = times;
    }

    @Override
    public void initialize(OptionalGiftActivity parent) {
        super.initialize(parent);
        if (rewards == null) {
            rewards = new HashMap<>();
        }
    }

    public int countItem() {
        return rewards.size();
    }

    public GameResult<Void> setReward(Map<Integer,Integer> rewards) {
        GameResult<Void> r = new GameResult<>();
        if (rewards.size() > getBaseRow().gridNum) {
            return r.fail("奖励数量不能多于格子");
        }
        for (Integer key:
             rewards.keySet()) {
            GameResult<Void> result = putOptional(key, rewards.get(key));
            if (!result.ok()) {
                rewards.clear();
                return result;
            }
        }
        return r;
    }

    public GameResult<Void> putOptional(int key, int value) {
        GameResult<Void> r = new GameResult<>();
        OptionalGiftPoolRow poolRow = getPoolRow(value);
        OptionalGiftBaseRow baseRow = getBaseRow(this.id);
        if (baseRow == null) {
            return r.fail("基础配置不存在");
        }
        if (poolRow == null) {
            return r.fail("奖池配置不存在");
        }
        if (baseRow.poolGroup != poolRow.group) {
            return r.fail("错误的奖励组");
        }
        if (key != poolRow.grid) {
            return r.fail("此格子无此奖励");
        }
        rewards.put(key, value);
        return r;
    }

    @JsonIgnore
    @Transient
    public OptionalGiftBaseRow getBaseRow() {
        return getBaseRow(this.id);
    }

    @JsonIgnore
    @Transient
    private OptionalGiftBaseRow getBaseRow(int id) {
        return role.getGame().table.optionalGift.base.get(id);
    }

    @JsonIgnore
    @Transient
    private OptionalGiftPoolRow getPoolRow(int id) {
       return role.getGame().table.optionalGift.pool.get(id);
    }

    public MixRes findRewards() {
        if (getBaseRow() == null) {
            return null;
        }
        MixRes reward = new MixRes(getBaseRow().baseReward);
        reward = rewards.values().stream().map(v -> new MixRes(getPoolRow(v).reward)).reduce(reward, MixRes::addList);
        return reward;
    }

    public void addTime() {
        this.times++;
    }

    public void clearReward() {
        rewards.clear();
    }
}
